package com.cino.org.controller;

import com.cino.basic.base.exception.BusinessException;
import com.cino.basic.util.ExcelUtils;
import com.cino.org.domain.ShopAuditLog;
import com.cino.org.dto.ShopDto;
import com.cino.org.service.ShopService;
import com.cino.org.domain.Shop;
import com.cino.org.query.ShopQuery;
import com.cino.basic.util.AjaxResult;
import com.cino.basic.util.PageList;
import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        }  catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.pageQuery(query);
    }

    /**
     * 接口：入驻
     * @param shop 查询对象
     * @return AjaxResult 响应给前端
     */
    @PostMapping(value="/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
    /**
     * 店铺审核通过
     * @param log
     * @return
     */
    @PostMapping(value="/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log){
        try {
            shopService.auditPass(log);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 店铺审核失败
     * @param log
     * @return
     */
    @PostMapping(value="/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log){
        try {
            shopService.auditReject(log);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @GetMapping(value="/export")
    public void exportExcel(HttpServletResponse response){//response 返回给前端的对象
        List<Shop> shops = shopService.loadAll();
        ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"系统数据.xlsx",response);

    }

    @PostMapping(value="/importExcel")
    public void importExcel(@RequestPart MultipartFile file){//从请求中获取多个参数
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(res ->{
            System.out.println(res);
        });
    }

    @GetMapping(value="/active/{id}")
    public String active(@PathVariable("id") Long id){//PathVariable 从请求路径中获取参数 一般都是一个参数
        try {
            shopService.updateState(id);
            return "<h1>恭喜你，账号激活成功!!</h1>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<h1>激活失败!!</h1>";
        }
    }

    /**
     * 接口：柱状图
     * @param
     */
    @GetMapping(value = "/echarts")
    public List<ShopDto> echarts() {
       return shopService.echarts();
    }
}

